class ServiceInformationTable < ActiveRecord::Base
  attr_accessible :id, :select_file_id
   
    belongs_to :select_file
  
    has_many :service_information, :dependent => :delete_all
    
  def self.to_csv
    idFileRedBee = SelectFile.where(:server => "RedBee").first.id
    idSIT = ServiceInformationTable.where(:select_file_id => idFileRedBee)
    idPLT = ProgramLocationTable.where(:select_file_id => idFileRedBee)
    listOfServices = ServiceInformation.where(:service_information_table_id=>idSIT).select(:id).map{|i| i.id }
    
    CSV.generate do |csv|
      csv << ['Name','Expected Language','Name','Language','Start','End','Name','Language','Start','End']
      for default_channel in DefaultChannel.all
        row = Array.new
        row << default_channel.name
        row << default_channel.language
        si = ServiceInformation.where(:service_information_table_id=>idSIT).where(:name=>default_channel.name).first
        if (si==nil)
          row << ''
          row << ''
          row << ''
          row << ''
        else
          row << si.name
          listOfServices.delete(si.id)
          row << ''
          row << Schedule.where(:program_location_table_id=>idPLT).where(:serviceIdRef=>si.serviceId).first.start
          row << Schedule.where(:program_location_table_id=>idPLT).where(:serviceIdRef=>si.serviceId).first.end
        end
        row << ''
        row << ''
        row << ''
        row << ''
        csv << row
    end 
  for serviceId in listOfServices
    row = Array.new
    row << ''
    row << ''
    row << ServiceInformation.find(serviceId).name
    row << ''
    row <<  Schedule.where(:serviceIdRef=>ServiceInformation.find(serviceId).serviceId).first.start
    row << Schedule.where(:serviceIdRef=>ServiceInformation.find(serviceId).serviceId).first.end
    row << ''
    row << ''
    row << ''
    row << ''
    csv << row
  end 
    end
  end

end
